<template>
  <div class="app-container">
    <el-form :model="formInline" ref="form" label-width="90px" class="form-inline" label-suffix="：">
      <el-row :gutter="10">
        <el-col :lg="6" :xl="4"><el-form-item label="保单号">
            <el-input v-model="formInline.insureNum" placeholder="请输入保单号"></el-input>
          </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="订单号">
            <el-input v-model="formInline.orderNum" placeholder="请输入订单号"></el-input>
          </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="箱号">
            <el-input v-model="formInline.containerNumber" placeholder="请输入箱号"></el-input>
          </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="运单号">
            <el-input v-model="formInline.tagInvoiceWaybill" placeholder="请输入运单号"></el-input>
          </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="批单状态">
            <el-select v-model="formInline.applyModifyStatus" placeholder="请选择批单状态">
              <el-option label="全部" value=""></el-option>
              <el-option label="申请" value="1"></el-option>
              <el-option label="申请通过" value="2"></el-option>
              <el-option label="申请失败" value="3"></el-option>

            </el-select>
          </el-form-item></el-col>
        <el-col :lg="1" :xl="1">
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table ref="table" :data="tableData">
      <el-table-column align="center" prop="insureNum" label="保单号" width="220">
      </el-table-column>

      <el-table-column align="center" prop="boxNo" label="箱号/运单号/车号/厢号" width="180">
        <template slot-scope="scope">
          <div v-if="!!scope.row.containerNumber">箱号：{{ scope.row.containerNumber }}</div>
          <div v-if="!!scope.row.emsNumber">运单号：{{ scope.row.emsNumber }}</div>
          <div v-if="!!scope.row.carNumber">车号：{{ scope.row.carNumber }}</div>
          <div v-if="!!scope.row.vanNumber">厢号：{{ scope.row.vanNumber }}</div>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="orderNum" label="订单号" width="220">
      </el-table-column>

      <el-table-column align="center" prop="insuredName" label="被保险人" width="180">
      </el-table-column>

      <el-table-column align="center" prop="modifyStatus" label="批单权限">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.modifyStatus === '0'">无权限</el-tag>
          <el-tag v-if="scope.row.modifyStatus === '1'" type="success">有权限</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="applyModifyStatus" label="批单状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.applyModifyStatus === '1'">申请</el-tag>
          <el-tag v-if="scope.row.applyModifyStatus === '2'" type="success">申请通过</el-tag>
          <el-tag v-if="scope.row.applyModifyStatus === '3'" type="danger">申请失败</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="reportStatus" label="保险出险状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.reportStatus === '0'">未立案</el-tag>
          <el-tag v-if="scope.row.reportStatus === '1'" type="warning">报案</el-tag>
          <el-tag v-if="scope.row.reportStatus === '2'" type="warning">立案</el-tag>
          <el-tag v-if="scope.row.reportStatus === '3'" type="danger">取消报案</el-tag>
          <el-tag v-if="scope.row.reportStatus === '4'" type="warning">勘察/定损</el-tag>
          <el-tag v-if="scope.row.reportStatus === '5'" type="warning">核赔</el-tag>
          <el-tag v-if="scope.row.reportStatus === '6'" type="success">付款</el-tag>
          <el-tag v-if="scope.row.reportStatus === '7'" type="success">结案</el-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column align="center" prop="orderNum" label="出险时间" width="220">
            </el-table-column> -->

      <el-table-column align="center" prop="reportStatus" label="订单状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderStatus === '2'">支付成功</el-tag>
          <el-tag v-if="scope.row.orderStatus === '3'" type="success">投保成功</el-tag>
          <el-tag v-if="scope.row.orderStatus === '4'" type="danger">投保失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="insureFee" label="保险金额(元)"> </el-table-column>
      <el-table-column align="center" prop="goodsName" label="货物名称"> </el-table-column>
      <el-table-column align="center" width="270" prop="payTime" label="投保时间"> </el-table-column>
      <el-table-column align="center" width="270" label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="view(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-success" v-if="scope.row.applyModifyStatus * 1 === 1" @click="agreeApply(scope.row.orderNum)">同意申请</el-button>
          <el-button size="mini" type="text" icon="el-icon-error" v-if="scope.row.applyModifyStatus * 1 === 1" @click="cancelApply(scope.row)">不同意申请</el-button>
          <el-button v-if="scope.row.applyModifyStatus * 1 === 2" size="mini" type="text" icon="el-icon-edit-outline" @click="editPermissions(scope.row.orderNum)">修改权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="margin-top: 20px" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page.pageNum" :page-sizes="[10, 20, 50]"
      :page-size="page.pageSize" layout="->, total, sizes, prev, pager, next, jumper" :total="page.total">
    </el-pagination>
    <view-or-examine @initTable="initTable" ref="viewOrExamine"></view-or-examine>

    <el-dialog title="取消订单" :visible.sync="showCancelOrder" v-loading="loading">
      <el-form :model="cancelOrderForm" ref="cancelOrderForm">
        <el-form-item label="订单取消原因备注" :label-width="150" prop="orderStatusRemark" :rules="[{ required: true, message: '请输入备注', trigger: 'blur' }]">
          <el-input type="textarea" v-model="cancelOrderForm.orderStatusRemark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showCancelOrder = false">取 消</el-button>
        <el-button type="primary" @click="confirmClick">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="不同意申请" :visible.sync="showCancelApply" v-loading="loading">
      <el-form :model="cancelApplyForm" ref="cancelApplyForm">
        <el-form-item label="备注" :label-width="150" prop="remark" :rules="[{ required: true, message: '请输入备注', trigger: 'blur' }]">
          <el-input type="textarea" v-model="cancelApplyForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showCancelApply = false">取 消</el-button>
        <el-button type="primary" @click="cancelApplyClick">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import viewOrExamine from "./viewOrExamine.vue";

export default {
  components: { viewOrExamine },
  data() {
    return {
      loading: false,
      showCancelApply: false,
      cancelApplyForm: {
        remark: "",
      },
      showCancelOrder: false,
      orderRow: {},
      cancelOrderForm: {
        orderStatusRemark: "",
      },
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      formInline: {
        insureNum: "",
        orderNum: "",
        containerNumber: "",
        tagInvoiceWaybill: "",
        applyModifyStatus: "",
      },
      tableData: [],
      tableHeight: 0,
      row: {},
    };
  },
  methods: {
    cancelApplyClick() {
      this.$refs.cancelApplyForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const params = {
            orderNum: this.row.orderNum,
            remark: this.cancelApplyForm.remark,
          };
          this.$post(`/warranty/modify/cancelApply`, params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
                this.showCancelApply = false;
                this.page.pageNum = 1;
                this.getTableData();
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    onSubmit() {
      this.page.pageNum = 1;
      this.getTableData();
    },
    agreeApply(orderNum) {
      this.$confirm("确认同意?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$post(`/warranty/modify/agreeApply`, { orderNum }).then(
            (res) => {
              if (res && res.code * 1 === 200) {
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
                this.page.pageNum = 1;
                this.getTableData();
              }
            }
          );
        })
        .catch(() => {
          //
        });
    },
    cancelApply(row) {
      this.row = row;
      this.showCancelApply = true;
    },
    editPermissions(orderNum) {
      this.$confirm("确认修改权限?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$post(`/warranty/modify/modifyApplyPermission`, {
            orderNum,
          }).then((res) => {
            if (res && res.code * 1 === 200) {
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              this.page.pageNum = 1;
              this.getTableData();
            }
          });
        })
        .catch(() => {
          //
        });
    },
    confirmClick() {
      const params = {
        warrantyId: this.orderRow.warrantyId,
        orderStatusRemark: this.cancelOrderForm.orderStatusRemark,
      };
      this.$refs.cancelOrderForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          this.$post(`/warranty/insure/cancelOrder`, params)
            .then((res) => {
              this.$message.success("取消成功"); // 成功提示信息
              this.showCancelOrder = false;
              this.page.pageNum = 1;
              this.getTableData();
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getTableData();
    },
    view(row) {
      this.$refs.viewOrExamine.dialogFormVisible = true;
      this.$refs.viewOrExamine.title = "查看";
      this.$refs.viewOrExamine.isExamine = false;
      this.$refs.viewOrExamine.getProcess(row.orderNum);
      this.$refs.viewOrExamine.row = row;
      this.$refs.viewOrExamine.beforeBatchOrder = row.frontModifyInsureRecord;
      this.$refs.viewOrExamine.frontModifyInsureHistory = row.applyModifyStatus === '2' ? row.frontModifyInsureHistory : row
      this.$refs.viewOrExamine.insureUrl = row.insureUrl;
      this.$refs.viewOrExamine.form.insureNum = row.insureNum;
    },
    setTableHeight() {
      this.$nextTick(() => {
        const queryConditionHeight = this.$refs.form.$el.offsetHeight;
        this.tableHeight =
          window.innerHeight - queryConditionHeight - 180 + "px";
      });
    },
    initTable() {
      this.$refs.viewOrExamine.dialogFormVisible = false;
      this.page.pageNum = 1;
      this.getTableData();
    },
    getTableData() {
      const params = {
        ...this.formInline,
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize,
      };
      console.log(params, params);
      this.$get("/warranty/modify/list", params).then((res) => {
        this.tableData = res.rows;
        this.page.total = res.total;
      });
    },
  },
  mounted() {
    window.onresize = () => {
      this.setTableHeight();
    };
    this.setTableHeight();
    this.getTableData();
  },
};
</script>
<style scoped lang="scss">
.form-inline ::v-deep {
  width: 100%;

  .el-form-item__label {
    // padding: 0;
  }
}
</style>